import Settings from '../config/Settings.js';
import Crow from './Crow.js';

export default class Enemy {
  constructor(game, type, options = {}) {
    // 如果是乌鸦类型，直接返回Crow实例
    if (type === 'crow') {
      return new Crow(game, options);
    }
    
    this.game = game;
    this.type = type;
    this.x = options.x || game.canvas.width;
    this.y = options.y || 0;
    
    const config = Settings.enemies.types[type];
    this.width = config.width;
    this.height = config.height;
    this.speed = config.speed;
    this.damage = config.damage;
    
    // 特殊属性
    if (type === 'fox') {
      this.jumpForce = config.jumpForce;
      this.velocity = 0;
      this.jumpTimer = 0;
    } else if (type === 'boar') {
      this.charging = false;
      this.chargeTimer = 0;
      this.chargeSpeed = config.chargeSpeed;
    }
    
    this.sprite = config.sprite;
  }
  
  update() {
    switch(this.type) {
      case 'fox':
        // 狐狸会跳跃移动
        this.x -= this.speed;
        this.velocity += Settings.player.gravity;
        this.y += this.velocity;
        
        // 到地面就跳
        if (this.y + this.height >= this.game.canvas.height - 60) {
          this.y = this.game.canvas.height - 60 - this.height;
          this.velocity = this.jumpForce;
        }
        break;
        
      case 'boar':
        // 野猪会冲刺
        if (!this.charging) {
          this.x -= this.speed;
          this.chargeTimer++;
          if (this.chargeTimer > 120) { // 每2秒冲刺一次
            this.charging = true;
            this.chargeTimer = 0;
          }
        } else {
          this.x -= this.chargeSpeed;
          this.chargeTimer++;
          if (this.chargeTimer > 30) { // 冲刺0.5秒
            this.charging = false;
            this.chargeTimer = 0;
          }
        }
        break;
    }
  }
  
  render(ctx) {
    ctx.save();
    
    switch(this.type) {
      case 'fox':
        // 绘制狐狸
        ctx.fillStyle = this.sprite.color;
        ctx.fillRect(this.x, this.y, this.width, this.height);
        // 绘制尾巴
        ctx.fillStyle = this.sprite.secondaryColor;
        ctx.beginPath();
        ctx.moveTo(this.x - 10, this.y + this.height/2);
        ctx.quadraticCurveTo(
          this.x - 20, 
          this.y + this.height/2 - 20,
          this.x - 30, 
          this.y + this.height/2
        );
        ctx.fill();
        break;
        
      case 'boar':
        // 绘制野猪
        ctx.fillStyle = this.sprite.color;
        ctx.fillRect(this.x, this.y, this.width, this.height);
        // 冲刺特效
        if (this.charging) {
          ctx.fillStyle = 'rgba(255,0,0,0.3)';
          ctx.fillRect(this.x - 20, this.y, 20, this.height);
        }
        break;
    }
    
    ctx.restore();
  }
  
  checkCollision(player) {
    const collision = (
      player.x + player.width/2 > this.x &&
      player.x - player.width/2 < this.x + this.width &&
      player.y + player.height > this.y &&
      player.y < this.y + this.height
    );
    
    if (collision) {
      // 如果玩家从上方跳到敌人头上
      if (player.verticalVelocity > 0 && player.y + player.height < this.y + this.height/2) {
        return 'defeat';
      } else {
        return 'damage';
      }
    }
    
    return false;
  }
} 